import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jyofservice/fluro/NavigatorUtil.dart';
import 'package:jyofservice/bean/histroy_list_entity.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:jyofservice/util/AesUtil.dart';

//同单元工单
class CommonUnitPage extends StatefulWidget {

  final String? sfbm;
  final String? xqid;
  CommonUnitPage({this.sfbm, this.xqid});

  @override
  _CommonUnitPageState createState() => _CommonUnitPageState();
}

class _CommonUnitPageState extends State<CommonUnitPage> {

  //单元号
  String _dyID = "";
  List<HistroyListDataData> histroyListData = [];
  ScrollController scrollController = ScrollController();
  int page = 1;
  bool isloading = false;


  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getHouseUnitData().then((data){

      print("getCommonUnitListData===$data");
      if(data!="") {

        getCommonUnitListData(data);
      }


    });

    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        print('滑动到了最底部');
        page++;
        getCommonUnitListData(_dyID);
      }
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.5,
        centerTitle: true,
        title: Text("同单元工单",
            style: TextStyle(
                color: Colors.white, fontSize: ScreenUtil().setSp(45))),
      ),

      body: Container(
          color: const Color(0xFFF2F5F9),
          child:Stack(
            alignment: Alignment(0.5, 0.5),
            children: <Widget>[
              RefreshIndicator(
                  child: ListView(

                    physics: AlwaysScrollableScrollPhysics(),
                    controller: scrollController,
                    children: <Widget>[
                      SingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        child:  DataTable(
                          columnSpacing: 20,
                          horizontalMargin: 20,
                          columns: [
                            DataColumn(
                                label: Text(
                                  '工单号',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '住户地址',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '工单类型',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '联系人',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '联系电话',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '来电号码',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '工单描述',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '下单时间',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '工单状态',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '处理部门',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '下发时间',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '处理人员',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '处理时间',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '处理结果',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '处理内容',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '满意度',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),
                            DataColumn(
                                label: Text(
                                  '回访内容',
                                  style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                                )),

                          ],
                          rows: getTableRow(),
                          //为用户提供行数选择
                        ),
                      )
                    ],
                  ),
                  onRefresh: onHeaderRefresh),
              Offstage(
                offstage: isloading,
                child: SpinKitFadingCircle(
                  color: Colors.blue,
                  size: 50.0,
                ),
              )
            ],
          )
      ),

    );
  }

  //刷新
  Future<Null> onHeaderRefresh() async {
    page = 1;
    histroyListData.clear();
    getCommonUnitListData(_dyID);
  }

  List<DataRow> getTableRow() {
    List<DataRow> list = [];
    for (int i = 0; i < histroyListData.length; i++) {
      list.add(DataRow(cells: [
        DataCell(Text(
          histroyListData[i].lSH??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].allName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].typeName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].linkMan??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].linkPhone??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].comePhone??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].describe??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].createDate??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].stateName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].departmentName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].xFTime??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].cLUserName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].cLTime??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].resultName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].cLRemarks??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].manYiDuName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].hFRemarks??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),

      ]));
    }
    print(list.length);

    return list;
  }

  //获取单元号
  Future getHouseUnitData() async {

    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    String aesData= await AesUtil.encodeBase64();
    var data = {
      "key": key,
      "view": "vw_Public_house",
      "wherestr": " Sfbm='" + widget.sfbm! +"'",
      "sortName": "House_id",
      "sortOrder": 'desc',
      "ClientType":"phone","ClientKey":aesData
    };

    Response response = await Dio().post(
        WebConfig().baseUrl + "DB/getrowdatanopage",
        data: data);

    print("${WebConfig().baseUrl + "DB/getrowdatanopage"}");
    print("同单元工单单元号:${response}==$data");

    if (response.data["Code"] == "200") {

      if(response.data["Data"]["count"] == "0"||response.data["Data"]["count"] == "") {

        if (mounted) {
          setState(() {
            isloading = true;
          });
        }
        Fluttertoast.showToast(msg: "未查询到数据");

      } else {

        _dyID = response.data["Data"]["data"][0]["Dy_id"].toString();


      }

      return _dyID;

    } else {
      Fluttertoast.showToast(msg: "当前请求数据错误请重新刷新");
    }


  }

  //获取历史列表数据
  Future getCommonUnitListData(String dyID) async {

    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    print("同单元工单key===$key==${widget.sfbm}");
    String aesData= await AesUtil.encodeBase64();
    var data = {
      "key": key,
      "view": "vw_GD_history_Dy",
      "wherestr": " Dy_id='" + dyID +"'",
      "limit": 10,
      "page": page,
      "sortName": "CreateDate",
      "sortOrder": "desc","ClientType":"phone","ClientKey":aesData
    };
    Response response = await Dio().post(
        WebConfig().baseUrl + "DB/postrowdataforlayui",
        data: data);

    print("${WebConfig().baseUrl + "DB/postrowdataforlayui"}");
    print("同单元工单:${response}==$data");

    if (response.data["code"] == 0) {
      for (int i = 0; i < response.data["data"].length; i++) {
        HistroyListDataData diaoDuListEntity =
        new HistroyListDataData.fromJson(response.data["data"][i]);
        histroyListData.add(diaoDuListEntity);
      }

      if (mounted) {
        setState(() {
          isloading = true;
        });
      }
    } else {
      if (mounted) {
        setState(() {
          isloading = true;
        });
      }
      Fluttertoast.showToast(msg: "当前请求数据错误请重新刷新");
    }
  }
}
